package com.shop.front.dao.store;


import com.shop.jpa.BaseSQLDao;
import com.shop.jpa.support.QuerySQL;
import com.shop.front.vo.store.StoreOrderUserStatisticVo;
import com.shop.front.vo.store.StoreOrderVo;
import com.shop.web.context.WebContext;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;

@Repository
public class StoreOrderSQLDao extends BaseSQLDao {

    public Page<StoreOrderVo> myOrder(Long userId, Integer status) {
        String field = "so.*";
        String from = "t_store_order so ";
        String order = "so.id desc";

        QuerySQL qs = new QuerySQL(WebContext.getPageNo(), WebContext.getPageSize(), field, from, order);
        qs.addWhereWithoutParams("so.is_deleted = 0");
        qs.addWhereWithoutParams("so.is_user_del = 0");
        qs.addWhereEq("so.uid", userId);
        qs.addWhereEqNotEmpty("so.status", status);

        return this.page(qs, StoreOrderVo.class);
    }

    /**
     * 我的团队业绩统计
     *
     * @param userId
     * @return
     */
    public StoreOrderUserStatisticVo myOrderStatistic(Long userId) {
        StringBuffer field = new StringBuffer(userId + "")
                .append(", ifnull(sum(case when so.pay_status = 0 and so.status = 0 then 1 else 0 end), 0) need_pay_num")
                .append(", ifnull(sum(case when so.pay_status = 1 and so.`status` = 10 then 1 else 0 end), 0) need_delivery_num")
                .append(", ifnull(sum(case when so.pay_status = 1 and so.`status` = 20 then 1 else 0 end), 0) need_take_delivery_num")
                .append(", ifnull(sum(case when so.pay_status = 1 and so.`status` = 30 then 1 else 0 end), 0) need_evaluation_num")
                .append(", ifnull(sum(case when so.pay_status = 1 and so.`status` = 40 then 1 else 0 end), 0) completed_num")
                .append(", ifnull(sum(case when so.pay_status = 1 then 1 else 0 end), 0) total_num")
                .append(", ifnull(sum(case when so.pay_status = 1 then 1 else 0 end), 0) total_price")
                ;
        String from = "t_store_order so";
        QuerySQL qs = new QuerySQL(field.toString(), from, null);
        qs.addWhereEq("so.uid", userId);
        return this.querySingle(qs, StoreOrderUserStatisticVo.class);
    }

}
